package DataObjects;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.Vector;

import javax.swing.JOptionPane;

import Messages.MessageAddWorkingHours;
import Messages.MessageAddWorkingHoursReply;
import Messages.MessageSavingReply;

public class TotalHoursInMonthAccess implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	 *  calculate working hours in month
	 */
//	private MainMenu mainMenu;
	private Vector<String> tempMonth;	
	private Vector <String> hours;	
	private Date dateOut;
	private Date dateIN;
	private int flag;
	
	private long totalDayTime;
	private long totalMonthTime;
	private long res1;
	private long res2;
	
	private String hoursOfDay;
	private String minutesOfDay;
	private String secondsOfDay;
	
	private String hoursOfMonth;
	private String minutesOfMonth;
	private String secondsOfMonth;
	
	public TotalHoursInMonthAccess(Vector<String> allHours4)
	{		 
		this.tempMonth=allHours4; 
	}

	
	public Vector <String> getwrkHRS() {
		return hours;
	}

	
	public void setwrkHRS(Vector <String> vecwrkHRS) {
		this.hours = vecwrkHRS;
	}

	public void CalculateMonthHours() throws ParseException
	{
		     
	      
	      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); //("yyyy-MM-dd HH:mm:ss");
		  
		  totalMonthTime=0;
		 
		  for(int i=0;i<tempMonth.size()/2;i++)
		   	 {
			  	 
              if(tempMonth.get(2*i+1) != null && !tempMonth.get(2*i+1).equals("empty"))
		       	 {
		     	 	       	 
   	             String inputStringout = (String) tempMonth.get(2*i+1);      //"00:01:30.500";
			         
 		         dateOut = sdf.parse(inputStringout);
 		           res1 = dateOut.getTime(); 
		       	 } 
        	          else flag = 1;                         // ne bil sovershen vihod , ne idet v schet chasov
		   		 
		   		 
         	     if(tempMonth.get(2*i)!= null && !tempMonth.get(2*i+1).equals("empty"))
		       	 {
		            
		       		
		          	String inputStringin = (String) tempMonth.get(2*i);      //"00:01:30.500";
			        
			        dateIN = sdf.parse(inputStringin);
			         res2 = dateIN.getTime();
		       	 } 
	 	       	 else flag=1;
		       	 
		 
	           // sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

		    //   	long res1 = dateOut.getTime();
			 //   long res2 = dateIN.getTime();
			  if(flag!=1)   {
		        if(res2>res1)
		        {
		        	String inputStringTwelwe = (String)"00:00:00";
		        	 
			        Date dateTwelwe = sdf.parse(inputStringTwelwe);
			         
			        long res3 = dateTwelwe.getTime();                                                //problema [solved]
				    
			        String inputStringTwelwe2 = (String)"23:59:59";
		        	 
			        Date dateTwelwe2 = sdf.parse(inputStringTwelwe2);
			       
			        long res4 = dateTwelwe2.getTime();                                                //problema [solved]
				    
			        long resultTwelwe1= (int)Math.abs(res4 - res2);             ///60000 ;
			       
			        long resultTwelwe2= (int)Math.abs(res1 - res3);            ///60000 ;
			        
			        long result= (int) (resultTwelwe1 + resultTwelwe2)  ;    // v minutah 
			        
			        totalMonthTime+=result;
		        }
		        
			 
		        else{	
		        	 totalMonthTime += (int)Math.abs(res1 - res2);      ///60000 ;    // v minutah 
			      
		          }
			  } flag=0;
		         
		   	 }// for 
	       
	        long diffSeconds = totalMonthTime / 1000;         
	        long diffMinutes = totalMonthTime / (60 * 1000);         
	        long diffHours = totalMonthTime / (60 * 60 * 1000);   
	        
	        diffMinutes= diffMinutes- diffHours*60;
	        diffSeconds= diffSeconds- (diffHours*3600+ diffMinutes*60);
	        
	      setHoursOfMonth(String.valueOf(diffHours));       
	      setMinutesOfMonth(String.valueOf(diffMinutes));
	      setSecondsOfMonth(String.valueOf(diffSeconds));
	    
		 
	}


	public String getHoursOfMonth() {
		return hoursOfMonth;
	}


	public void setHoursOfMonth(String hoursOfMonth) {
		this.hoursOfMonth = hoursOfMonth;
	}


	public String getMinutesOfMonth() {
		return minutesOfMonth;
	}


	public void setMinutesOfMonth(String minutesOfMonth) {
		this.minutesOfMonth = minutesOfMonth;
	}


	public String getSecondsOfMonth() {
		return secondsOfMonth;
	}


	public void setSecondsOfMonth(String secondsOfMonth) {
		this.secondsOfMonth = secondsOfMonth;
	}


	public String getHoursOfDay() {
		return hoursOfDay;
	}


	public void setHoursOfDay(String hoursOfDay) {
		this.hoursOfDay = hoursOfDay;
	}


	public String getMinutesOfDay() {
		return minutesOfDay;
	}


	public void setMinutesOfDay(String minutesOfDay) {
		this.minutesOfDay = minutesOfDay;
	}


	public String getSecondsOfDay() {
		return secondsOfDay;
	}


	public void setSecondsOfDay(String secondsOfDay) {
		this.secondsOfDay = secondsOfDay;
	}
	
	
}
